package com.study.happystudy.controller;

import com.study.happystudy.entity.ResultEntity;
import com.study.happystudy.po.File;
import com.study.happystudy.service.FileService;
import com.study.happystudy.tool.ColorChange;
import jakarta.annotation.Resource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("file/")
public class FileController {
    @Resource
    private FileService fileService;

    /**
     * 处理文件上传的请求。
     *
     * @param file 文件内容，以字符串形式传输，需要进行相应的解码和处理。
     * @return 根据文件上传的结果返回不同的响应。如果上传成功，返回"File uploaded successfully"；如果上传失败，返回错误信息。
     */
    // 上传
    @PostMapping("upload")
    public String uploadFile(
            @RequestParam("fileName") String fileName,
            @RequestParam("fileType") String fileType,
            @RequestParam("file") MultipartFile file) {
        try {
            return ResultEntity.Success(1,fileService.uploadFile(fileName, fileType, file));
//            return ResponseEntity.ok( fileService.uploadFile(fileName, fileType, file));
        } catch (Exception e) {
            return ResultEntity.Fail(-1,"文件上传失败");
//            return ResponseEntity.status(500).body("Failed to upload file");
        }
    }

    // 复制到课程
    @PostMapping("copyToCourse")
    public ResponseEntity<String> copyFileToCourse(
            @RequestParam("courseId") int courseId,
            @RequestParam("fileId") int fileId) {
        fileService.copyFileToCourse(courseId, fileId);
        return ResponseEntity.ok("好了");
    }

    @PostMapping("copyToUser")
    public ResponseEntity<String> copyFileToUser(
            @RequestParam("userId") int userId,
            @RequestParam("fileId") int fileId) {
        try {
            fileService.copyFileToUser(userId, fileId);
            return ResponseEntity.ok("好了");
        } catch (Exception e) {
            return ResponseEntity.status(500).body("不对");
        }
    }

    @DeleteMapping("deleteFromCourse")
    public ResponseEntity<String> deleteFileFromCourse(
            @RequestParam("courseId") int courseId,
            @RequestParam("fileId") int fileId) {
        try {
            fileService.deleteFileFromCourse(courseId, fileId);
            return ResponseEntity.ok("好了");
        } catch (Exception e) {
            return ResponseEntity.status(500).body("不对");
        }
    }

    @DeleteMapping("deleteFromUser")
    public ResponseEntity<String> deleteFileFromUser(
            @RequestParam("userId") int userId,
            @RequestParam("fileId") int fileId) {
        try {
            fileService.deleteFileFromUser(userId, fileId);
            return ResponseEntity.ok("好了");
        } catch (Exception e) {
            return ResponseEntity.status(500).body("不对");
        }
    }

    @GetMapping("getById")
    public File getFileById(@RequestParam("fileId") int fileId) {
        File file = fileService.getFileById(fileId);
        ColorChange.Red(file.getFileName());
        return file;
    }

    @DeleteMapping("deleteById")
    public ResponseEntity<String> deleteFileById(@RequestParam("fileId") int fileId) {
        fileService.deleteFileById(fileId);
        return ResponseEntity.ok("File deleted successfully");
    }

    @GetMapping("getByNameInCourse")
    public ResponseEntity<List<File>> getFilesByNameInCourse(
            @RequestParam("fileName") String fileName,
            @RequestParam("courseId") int courseId) {
        List<File> files = fileService.getFilesByNameInCourse(fileName, courseId);
        return ResponseEntity.ok(files);
    }

    @GetMapping("getByNameInUser")
    public ResponseEntity<List<File>> getFilesByNameInUser(
            @RequestParam("fileName") String fileName,
            @RequestParam("userId") int userId) {
        List<File> files = fileService.getFilesByNameInUser(fileName, userId);
        return ResponseEntity.ok(files);
    }

    @GetMapping("getByUserId")
    public ResponseEntity<List<File>> getFilesByUserId(@RequestParam("userId") int userId) {
        List<File> files = fileService.getFilesByUserId(userId);
        return ResponseEntity.ok(files);
    }

    @GetMapping("getByCourseId")
    public ResponseEntity<List<File>> getFilesByCourseId(@RequestParam("courseId") int courseId) {
        List<File> files = fileService.getFilesByCourseId(courseId);
        return ResponseEntity.ok(files);
    }

}
